package com.bby.disk.manager.upload;

import com.bby.common.config.GlobalSettings;
import com.bby.disk.bean.req.UploadTaskInfo;
import lombok.Data;

import java.io.File;
import java.io.Serializable;

@Data
public class UploadTask extends UploadTaskInfo implements Serializable {
    private String userId;
    // 用于清理过期任务
    private long activeTime;
    private File tempFile;


    public UploadTask(String guid, String userId, UploadTaskInfo request) {
        super(request);
        activeTime = System.currentTimeMillis();
        this.userId=userId;
        tempFile = new File(GlobalSettings.TEMP_FILES_DIR, guid);
        if (tempFile.exists()){
            tempFile.delete();
        }
    }
    public long lookupLoaded() {
        synchronized (this){
            return tempFile.exists()?
                    tempFile.length():0;
        }
    }

    public boolean isFileOk() {
        if (chunked){
            return false;
        }else {
            return lookupLoaded()==fileInfo.getSize();
        }
    }

//
//    /**
//     * 每个分片上传完都执行一下刷新
//     * @throws Exception
//     */
//    public synchronized boolean addChunk(String chunkId,String path) throws Exception {
//        finishedMap.put(chunkId,path);
//        return finishedMap.size() >= chunks;
//    }


}
